package cd.cdyb.sms.dao;

import cd.cdyb.sms.domain.InventoryFlow;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.time.LocalDateTime;

@Mapper
public interface InventoryFlowDao extends BaseMapper<InventoryFlow> {

    /**
     * 查询库存合计
     *
     * @param companyKey 公司KEY
     * @param direction 流水方向
     * @param startLocalDateTime 开始时间
     * @param endLocalDateTime 结束时间
     * @return 库存合计
     */
    @Select("<script>select IFNULL(sum(inventory_value), 0) from inventory_flow where company_key = #{companyKey} and whether_recording = false and type = #{type} and direction = #{direction} " +
            "and create_time <![CDATA[>=]]> #{startLocalDateTime} and create_time <![CDATA[<=]]> #{endLocalDateTime}</script>")
    Integer getInventoryFlowCountByCompanyId(@Param("companyKey") String companyKey, @Param("direction") Integer direction, @Param("type") Integer type,
                                             @Param("startLocalDateTime") LocalDateTime startLocalDateTime,
                                             @Param("endLocalDateTime") LocalDateTime endLocalDateTime);


    /**
     * 编辑库存流水状态
     *
     * @param companyKey 公司KEY
     * @param startLocalDateTime 开始时间
     * @param endLocalDateTime 结束时间
     */
    @Update("<script>update inventory_flow set whether_recording = true where company_key = #{companyKey} and whether_recording = false  " +
            "and create_time <![CDATA[>=]]> #{startLocalDateTime} and create_time <![CDATA[<=]]> #{endLocalDateTime}</script>")
    void updateInventoryFlowStatus(@Param("companyKey") String companyKey, @Param("startLocalDateTime") LocalDateTime startLocalDateTime,
                                   @Param("endLocalDateTime") LocalDateTime endLocalDateTime);
}
